$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/article/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '文章标题', name: 'articlename', index: 'articleName', width: 80 },
            { label: '浏览量', name: 'browsecount', index: 'browseCount', width: 80 },
			{ label: '操作时间', name: 'createtime', index: 'createtime', width: 80 },
            { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.update("+ rowdata.id +")'>" +
                        "编辑</a>&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.del("+ rowdata.id +")'>" +
                        "删除</a>&nbsp;&nbsp;"+
                        "<a style='cursor:pointer;' onclick='vm.setTuiGuang()'>" +
                    	"推广</a>&nbsp;&nbsp;";
            }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			title:null
		},
		showList: true,
		title: null,
		article: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.article = {};
            UE.getEditor('myEditor').setContent("")
		},
		update: function (id) {

			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
        setTuiGuang: function () {
            $('#myModal').modal();
        },
		saveOrUpdate: function (event) {
			var url = vm.article.id == null ? "sys/article/save" : "sys/article/update";
            //实例化编辑器

            vm.article.content = UE.getEditor('myEditor').getContent();
			if(vm.article.articlename == '' || typeof vm.article.articlename == 'undefined'){
                alert("文章标题不能为空");
			}else if(vm.article.content == '' ||typeof vm.article.content == 'undefined'){
                alert("文章内容不能为空");
			}else{
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.article),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
			}

		},
		del: function (id) {

			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/article/delete/"+id,
                    contentType: "application/json",
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/article/info/"+id,
                success: function(r){
                    vm.article = r.article;
                    UE.getEditor('myEditor').setContent(vm.article.content);

                }
            });

		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'title': vm.q.title},
                page:page
            }).trigger("reloadGrid");
		}
	}
});